package com.ymatou.promotion.api.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.ReceiveCouponInfoBean;
import com.ymatou.promotion.api.service.ReceiveCouponInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 用户领取优惠券
 *
 * 陆艳军 加了 领取优惠券的开关
 *
 * 开关在商家后台中开启，相应Sql如下：
 * select top 10 * from Ymt_CouponSellerPrefix where sPrefix in
 * (
 * SELECT sBatchPrefix FROM Ymt_CouponBatch where sBatchCode='1604156950'
 * )
 *
 * @author chenjiazhu
 */
@TestSuite
public class Ts_ReceiveCouponInfo {
    private static ReceiveCouponInfoBean receivecouponinfoBean;
    private static ReceiveCouponInfoCall receivecouponinfoCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("用户领取优惠券");
    }

    @Before
    public void caseUp() {
        receivecouponinfoBean = new ReceiveCouponInfoBean();
        receivecouponinfoCall = new ReceiveCouponInfoCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(receivecouponinfoCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_001() {
        Logger.start(true, "领取全场满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerDefaultCoupon();

            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_002() {
        Logger.start(true, "领取商品满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon();
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_003() {
        Logger.start(true, "领取洋码头PC端全场满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("1");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_004() {
        Logger.start(true, "领取洋码头WAP端全场满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("2");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_005() {
        Logger.start(true, "领取洋码头APP全场满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("3");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_006() {
        Logger.start(true, "领取神器APP全场满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("4");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_007() {
        Logger.start(true, "领取神器WAP全场满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("5");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_008() {
        Logger.start(true, "领取洋码头PC端商品满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon("1");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_009() {
        Logger.start(true, "领取洋码头WAP端商品满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon("2");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_010() {
        Logger.start(true, "领取洋码头APP商品满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon("3");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_011() {
        Logger.start(true, "领取神器APP商品满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon("4");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_012() {
        Logger.start(true, "领取神器WAP商品满减优惠券");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon("5");
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_013() {
        Logger.start(false, "验证BuyerUserId空时报错");
        try {
            setReceivecouponinfoBean();
            receivecouponinfoBean.setBuyerUserId(0);

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("当前用户不存在", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_014() {
        Logger.start(false, "验证BatchCode空时报错");
        try {
            setReceivecouponinfoBean();
            receivecouponinfoBean.setBatchCode("");

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("批次号无效(批次不存在或批次已作废)", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_015() {
        Logger.start(false, "验证BatchCode不存在时报错");
        try {
            setReceivecouponinfoBean();
            receivecouponinfoBean.setBatchCode("1234");

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("批次号无效(批次不存在或批次已作废)", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfo_016() {
        Logger.start(false, "反例-验证BuyerUserId不存在时报错-接口不做有效性验证，所以还是可以pass");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon();
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoBean.setBuyerUserId(1122546);;

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            VerifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ReceiveCouponInfo_017() {
        Logger.start(false, "反例-验证ValidStart格式不正确时报错-fail，需后期修复");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon();
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoBean.setValidStart("201614363");

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("ValidStart格式不正确", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ReceiveCouponInfo_018() {
        Logger.start(false, "反例-验证ValidEnd格式不正确时报错-fail，需后期修复");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon();
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoBean.setValidEnd("201614363");

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("ValidEnd格式不正确", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ReceiveCouponInfo_019() {
        Logger.start(false, "反例-验证ValidStart不正确时报错-fail，需后期修复");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon();
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoBean.setValidStart("2016-2-30");

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("ValidStart不正确", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ReceiveCouponInfo_020() {
        Logger.start(false, "反例-验证ValidEnd不正确时报错-fail，需后期修复");
        try {
            String batchCode = new CouponCallService().createSellerProductCoupon();
            setReceivecouponinfoBean(batchCode);

            receivecouponinfoBean.setValidEnd("2016-2-30");

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("ValidEnd不正确", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ReceiveCouponInfo_021() {
        Logger.start(false, "验证当前优惠券批次不可领取-作废，没有无门槛领取功能");
        try {

            CouponCallService couponCallService = new CouponCallService();

            CouponBatchBean couponBatchBean = couponCallService.setCouponBatchBean(1, "1", 1, 1);
            String batchCode = couponCallService.createSellerCoupon(couponBatchBean).getBatchCode();

            receivecouponinfoBean.setBatchCode(batchCode);
            receivecouponinfoBean.setBuyerUserId(Integer.parseInt(EnvSetup.getData("BuyUserId")));

            receivecouponinfoCall.setData(receivecouponinfoBean);
            receivecouponinfoCall.callService();

            Logger.verifyEquals("500", receivecouponinfoCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals("当前商家优惠券批次" + batchCode + "不可领取", receivecouponinfoCall.getMsg(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 根据传入batchCode设置请求参数bean
     * 
     * @param batchCode
     */
    private void setReceivecouponinfoBean(String batchCode)
    {
        // RA-2086:领取优惠券去掉无门槛限制，无门槛限制仅用于显示
        // new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode);
        receivecouponinfoBean.setBuyerUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
        receivecouponinfoBean.setBatchCode(batchCode);
    }

    /**
     * 数据库Ymt_CouponBatch表中取第一条记录中的batchcode设置请求参数bean
     * 
     * @return
     */
    private Map setReceivecouponinfoBean()
    {
        Logger.comment("数据库Ymt_CouponBatch表中取第一条记录中的batchcode设置请求参数bean");
        Map item = new YmtCouponBatchWapper().getTop1CouponBatch().get(0);

        receivecouponinfoBean.setBuyerUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
        receivecouponinfoBean.setBatchCode(item.get("sBatchCode").toString());
        return item;
    }

    private void VerifyReturn()
    {
        Logger.verifyEquals("200", receivecouponinfoCall.getCode(), "验证返回code");
        Logger.verifyEquals("0", receivecouponinfoCall.getBCode(), "验证返回Bcode");
        Logger.verifyEquals("用户领取私有优惠券成功", receivecouponinfoCall.getMessageText(), "验证返回MessageText");

        String couponCode = receivecouponinfoCall.getCouponCode();

        YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(couponCode).get(0);
        YmtCouponPrivateUserBound couponPrivateUserBound =
                new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(couponCode).get(0);
        YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectByPrimaryKey(coupon.getiBatchId());
        YmtCouponValue couponValue =
                new YmtCouponValueWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
        YmtCouponSetting couponSetting =
                new YmtCouponSettingWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
        YmtCouponScenario couponScenario =
                new YmtCouponScenarioWapper().selectByiCouponScenarioId(couponSetting.getiScenarioId()).get(0);

        // 验证数据库
        Logger.verifyEquals(receivecouponinfoBean.getBuyerUserId(), couponPrivateUserBound.getiUserId(),
                "验证数据库表Ymt_CouponPrivateUserBound中iUserId");
        Logger.verifyEquals(receivecouponinfoBean.getBatchCode(), couponBatch.getsBatchCode(),
                "验证数据库表Ymt_CouponBatch中BatchCode");
        Logger.verifyEquals(couponPrivateUserBound.getiBatchId(), couponBatch.getiBatchId(),
                "验证数据库表Ymt_CouponBatch与表Ymt_CouponPrivateUserBound中iBatchId");

        Logger.verifyEquals(1, couponSetting.getiReceiveCount(), "验证数据库表Ymt_CouponSetting中iReceiveCount");

        // 验证返回
        Logger.verifyEquals(receivecouponinfoBean.getBatchCode(), receivecouponinfoCall.getBatchCode(), "验证返回BatchCode");

    }
}
